//
//  SSMealSpecificationViewController.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/2/21.
//

import UIKit
import SwiftyJSON
import SwiftUI

class SSSingleMealViewController: UIViewController {
    
    var items: Array<ListCombo> = []
    @IBOutlet weak var tableView: UITableView!
    var id :String = ""
    var rootId = "1"

    override func viewDidLoad() {
        super.viewDidLoad()
        let topView = SSPresentTopView.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 228))
        topView.setType(type: rootId)
        tableView.tableHeaderView = topView
        tableView.register(UINib(nibName: "SSHomeTableViewCell", bundle: Bundle.init(for: SSHomeTableViewCell.self)), forCellReuseIdentifier: "SSHomeTableViewCell")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        getListNetwork()
    }


    @IBAction func closeAction(_ sender: Any) {
        dismiss(animated: true)
    }
    
    //MARK: -networing
    //list
    func getListNetwork() {
        let param = [
            "parentId": self.id
        ] as [String:AnyObject]
        SSProductRequest.getSingleProductRequest(parameters: param) { items in
            self.items = items
            self.tableView.reloadData()
        }

    }
    

}

extension SSSingleMealViewController: UITableViewDelegate, UITableViewDataSource
{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//        let item :ListCombo =  self.items[indexPath.row]
//        
//        let vc =  SSItemDetailViewController.init(nibName: "SSItemDetailViewController", bundle: nil)
//        vc.id = item.id
//        present(vc, animated: true)
    }
//    func numberOfSections(in tableView: UITableView) -> Int {
//        return self.items.count
//    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 42
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        var title = ""
        if self.items.count > 0 {
            title = (self.items[section] ).name
        }
        let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 42))
        view.backgroundColor = UIColor.white
        let label = UILabel()
        label.text = title
        label.font = UIFont.systemFont(ofSize: 18, weight: .bold)
        label.textColor = UIColor.black
        view.addSubview(label)
        label.snp.makeConstraints { make in
            make.width.equalTo(150)
            make.height.equalTo(26)
            make.centerY.equalTo(view.frame.size.height/2)
            make.left.equalTo(16)
        }
        
        return view
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SSHomeTableViewCell") as! SSHomeTableViewCell
        cell.setData(self.items[indexPath.row])
        cell.setType(type: rootId)
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 170
    }
}
